import json
import math
import urllib.request
from urllib.parse import quote

if __name__ == '__main__':
    # ["T","MTKWATCH","358688000039858","get_city_status","460","1","9493","47571",""]
    # data = '{T*MTKWATCH*358688000039867*get_txt*/mongo/5ab23d68e02fb07a94483e59}'
    data = '{T*MTKWATCH*877777777777775*wifi*cl=460,0,9784,4601,23;460,0,9784,4596,16&wl=c:4b:54:9b:8e:47,-70;28:ff:3e:a3:ad:d7,-92;bc:5f:f6:c:85:44,-89;c2:95:8e:2a:41:21,-81;b0:95:8e:2a:41:21,-84;c8:8d:83:9f:66:bc,-46}'
    # data = '{T*MTKWATCH*877777777777775*get_city_status*460*0*9360*4091*wgs84}'
    data = data.strip()
    data = data[1:-1]
    data_arr = data.split('*')
    cmd = data_arr[3]
    r = quote(data)

    req = urllib.request.Request('http://118.24.98.157:5002/mtk/' + cmd + '?r=' + r)
    response = urllib.request.urlopen(req, timeout=10)
    res_str = response.read().decode('utf-8')
    json_obj = json.loads(res_str)
    print(json_obj)


def test(location):
    if location.find("http://") >= 0:
        return location

    arr_num = int(location[:1])  # 4
    print(arr_num)
    use_str = location[1:]  # dsfadfadfads
    print(use_str)
    line_char_num = math.floor(len(use_str) / arr_num)  # 38
    print(line_char_num)
    last_line_more_letters_num = len(use_str) % arr_num
    print(last_line_more_letters_num)
    f = []
    new_offset = 0
    for g in range(last_line_more_letters_num):
        offset = g * line_char_num + g
        new_offset = offset + line_char_num + 1
        f.append(use_str[offset:new_offset])
    f.append(use_str[new_offset:])

    h = ''
    for g in range(len(f[0])):
        for i in range(len(f)):
            h += f[i][g:g + 1]

    from urllib.parse import unquote
    h = unquote(h)

    h = h.replace("^", "0")
    h = h.replace("+", " ")

    return h
#
#
# print(test("4%22i.%8F%12951155957pFhy12%5551dafbadaf2F8an2932%9%%78715E13a_%585EEEa4a3%9%c4Fm.meF%8F575297_394"
#            ".%uk339E---5ba45c5bf%1xit3292E8EF8919%8m3teD62%%%c6368EcE1"))
